<%-- 
    Document   : principal
    Created on : 26-may-2009, 12:18:42
    Author     : Alberto
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false" import="domain.*,dao.*,planner.*,java.sql.Date, java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>DOMECO</title>

    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>


<%if (request.getSession(false) != null) {%>
<table align="center" width="1000" border="0">
<tr>
<td>
<div id="menu" align="center"><jsp:include page="./menu principal.jsp"></jsp:include></div>
<span id="blueBig" >Bienvenido

    <%

    Userdom u = (Userdom) request.getSession().getAttribute("user");
    //Esto es una prueba
    String nick = new Operations().getUser(u.getNick()).getNick();


    %><%=nick%>
</span>


<div id="general">

    <div id="cuenta" align="center" >
                            <%
    Userdom user = (Userdom) request.getSession().getAttribute("user");
    Operations o = new Operations();
    Set cuentas = o.getBankaccountsList(user);
    double saldoTotal = 0;
    String name = "";
    boolean ocultar = false;
    boolean facturas = false;
    double totalI = 0;
    double totalE = 0;
    double totalFacturas = 0;
    if (cuentas.isEmpty()) {
        ocultar = true;
                            %>
                            <br>
                            <br>
                            <p align=right"><span id="rojo"> Noy hay Cuentas</span></p>
                            <%} else {
                                Iterator it1 = cuentas.iterator();
                                while (it1.hasNext()) {
                                    Bankaccount b = (Bankaccount) it1.next();
                                    name = b.getName();

                            %>
                                <p align=center"> <span id="datosCuenta"> Nombre de la Cuenta: <%=name%> </span> </p>
                                <br>
                                <table id="tabla" align="center" width="120px">

                                    <tr id="campoAzul" align="center">
                                        <td width="180px" align="center"><span id="blanco">Ingresos</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>
                                    <%
                                Set incomes = b.getIncomes();
                                boolean bool = false;

                                //Vamos a iterar sobre los ingresos
                                if (!incomes.isEmpty()) {
                                    Iterator it2 = incomes.iterator();
                                    totalI = 0;
                                    while (it2.hasNext()) {
                                        Income i = (Income) it2.next();
                                        if (i.getDateIncome().getMonth() == Integer.parseInt(request.getParameter("month")) && i.getDateIncome().getYear() == new java.util.Date().getYear()) {
                                            bool = true;
                                            double amount = i.getAmount();
                                            saldoTotal += amount;
                                            totalI += amount;
                                            String concept = i.getConcept();
                                            java.sql.Date date = i.getDateIncome();
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span onclick="abrirVentanaIngreso()" ><%=concept%></span></td>
                                        <td><span><%=date%></span></td>
                                        <td><span><%=amount%>€<a href="Delete?type=income&idIncome=<%out.print(i.getIdIncome());%>&redir=C">   <img src="./Imagenes/borrar.jpg"></a></span></td>
                                    </tr>
                                    <%
                                            }
                                        }
                                        //Ahora especificamos el total de ingresos
                                        if (bool) {
                                            List contratos = o.getWContrList(user);
        if(!contratos.isEmpty()){
            Iterator it4 = contratos.iterator();
            while(it4.hasNext()){
                Wcontract cont = (Wcontract) it4.next();
                Salary sal = new Operations().getSalaryDate(cont);
                if(sal == null){
                    totalI += cont.getBasicSalary();%> <span id="rojo">Incluido el Salario Básico de su contrato de trabajo. Revise el Salario de este mes.</span><br><br> <%
                }else if (sal.getEnumeration() == 2){
                    totalI += sal.getAmount();
                    %>  <span id="rojo">Incluido el Salario revisado.</span><br><br>  <%
                }else if (sal.getEnumeration() == 1){
                    %>  <span id="rojo">Salario revisado, pero no cobrado. Sueldo no incluido en la cuenta.</span><br><br>  <%
}}}

                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Ingresos: +<% out.println(totalI);%> €</span></td>
                                    </tr>
                                    <%} else {%>
                                    <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Ingresos en <%=b.getName()%></span> </td><td></td></tr>

                                    <%  }


                                    } else {
                                    %>
                                    <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Ingresos en <%=b.getName()%></span> </td><td></td></tr>
                                    <%                                }%>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Gastos</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>
                                    <%Set expenses = b.getExpenses();
                                bool = false;
                                //Ahora vamos a iterar sobre los gastos
                                if (!expenses.isEmpty()) {
                                    Iterator it2 = expenses.iterator();
                                    totalE = 0;
                                    while (it2.hasNext()) {
                                        Expense e = (Expense) it2.next();
                                        if (e.getDateExpense().getMonth() == Integer.parseInt(request.getParameter("month")) && e.getDateExpense().getYear() == new java.util.Date().getYear()) {
                                            bool = true;
                                            double amount = e.getAmount();
                                            saldoTotal -= amount;
                                            totalE += amount;
                                            String concept = e.getConcept();
                                            java.sql.Date date = e.getDateExpense();
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span onclick="abrirVentanaIngreso()" ><%=concept%></span></td>
                                        <td><span><%=date%></span></td>
                                        <td><span><%=amount%>€<a href="Delete?type=expense&idExpense=<%out.print(e.getIdExpense());%>&redir=C">            <img src="./Imagenes/borrar.jpg"></a></span></td>
                                    </tr>
                                    <%
                                            }
                                        }
                                        //Ahora especificamos el total de los gastos
                                        if (bool) {
                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Gastos: <% out.println(totalE);%> €</span></td>
                                    </tr>
                                    <%} else {%>
                                    <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Gastos en <%=b.getName()%></span> </td><td></td></tr>

                                    <%  }


                                    } else {
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Gastos en <%=b.getName()%></span> </td><td></td></tr>
                                    </tr>
                                    <%                                }%>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Facturas</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>

                                    <%//Recorremos las facturas de Electricidad
                                List l = o.getElecContrList(user);
                                Iterator itElec = l.iterator();
                                totalFacturas = 0;
                                while (itElec.hasNext()) {
                                    Electricitycontract e = (Electricitycontract) itElec.next();
                                    double power = e.getContratedPower();

                                    Electricityconsumption eCons = new Operations().getLastElecConsumption(e);
                                    if (eCons != null) {
                                        totalFacturas += eCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Electricidad)  <%=e.getCompany()%></span></td>
                                        <td><span><%=eCons.getDateFrom()%> --- <%=eCons.getDateTo()%></span></td>
                                        <td><span><%=eCons.getInvoice().getAmount()%> €</span></td>
                                    </tr>

                                    <%}
                                }
                                //Recorremos las facturas de Hipotecas
                                List lh = o.getMortgageContrList(user);
                                Iterator it1h = lh.iterator();
                                while (it1h.hasNext()) {
                                    Mortgagecontract e = (Mortgagecontract) it1h.next();

                                    Mortgageconsumption mCons = new Operations().getLastMortgConsumption(e);
                                    if (mCons != null) {
                                        totalFacturas += mCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Hipoteca)  <%=e.getCompany()%></span></td>
                                        <td><span><%=mCons.getDateFrom()%> --- <%=mCons.getDateTo()%></span></td>
                                        <td><span><%=mCons.getInvoice().getAmount()%> €</span></td>
                                    </tr>

                                    <%
                                    }
                                }
                                //Recorremos las facturas de Seguros
                                List ls = o.getInsuranceContrList(user);
                                Iterator it1s = ls.iterator();
                                while (it1s.hasNext()) {
                                    Insurancecontract e = (Insurancecontract) it1s.next();
                                    Insuranceconsumption iCons = new Operations().getLastInsConsumption(e);
                                    if(iCons != null){
                                        totalFacturas += iCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Seguro)  <%=e.getCompany()%></span></td>
                                        <td><span><%=iCons.getDateFrom() %> --- <%=iCons.getDateTo() %></span></td>
                                        <td><span><%=iCons.getInvoice().getAmount() %> €</span></td>
                                    </tr>

                                    <%
                                                    }
                                                }
                                //Recorremos las facturas de agua
                                List lw = o.getWaterContrList(user);
                                Iterator it1w = lw.iterator();
                                while (it1w.hasNext()) {
                                    Watercontract e = (Watercontract) it1w.next();
                                    Waterconsumption wCons = new Operations().getLastWaterConsumption(e);
                                    if(wCons != null){
                                        totalFacturas += wCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Agua)   <%=e.getCompany() %></span></td>
                                        <td><span><%=wCons.getDateFrom()%> --- <%=wCons.getDateTo()%></span></td>
                                        <td><span><%=wCons.getInvoice().getAmount() %> €</span></td>
                                    </tr>

                                    <%
                                                    }
                                                }



                                if (facturas) {
                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Facturas: <% out.println(totalFacturas);%> €</span></td>
                                    </tr>
                                    <%

                                }
                                //Calcular el saldo total:
                                saldoTotal = saldoTotal - totalFacturas;


                                    %>
                                </table>
                                <table id="" align="center">
                                    <tr><td><br></td></tr>

                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Saldo a su favor</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"><% out.println(saldoTotal);%> €</span></td>
                                    </tr>

                                </table>

                            <%
        }
    }
                            %>
                            

                        </div>



    <div id="barraDer">
        <div id="planificador">
            <table align="center">
                <tr ><td bgcolor="#79A9D9" align="center" valign="middle" width="290px"><span id="blanco">Planificador</span><br></td></tr>
            </table>
            <br>
            <table align="center">
                <tr ><td bgcolor="#79A9D9" align="center" valign="middle" width="235px"><span id="blanco">Tareas</span><br></td></tr>
            </table>
            <%

    Set sT = new Operations().getTasksList(u);
    String atrasados = null;
    Iterator itT = sT.iterator();
    if (sT.isEmpty()) {
            %><p align="center"> <span id="azul">No hay Tareas</span></p><%                                            } else {
                                                while (itT.hasNext()) {
                                                    Task t = (Task) itT.next();
                                                    if (t.getDateExpired().after(new java.util.Date())) {%>
            <p align="center"><a href="" id="colorEvento" name="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'><% out.print(t.getTask());%> <%out.print(t.getDateExpired());%></span></a><a href="./Delete?idTask=<%=t.getIdTask()%>&type=task&redir=P1"><img src="/Imagenes/borrar.jpg"></a></p>
            <%} else {%>
            <p align="center"><a href="" id="aviso" name="colorEvento"><span><% out.print(t.getTask());%> <%out.print(t.getDateExpired());%> (Expirado)</span></a>  <a href="./Delete?idTask=<%=t.getIdTask()%>&type=task&redir=P1"><img src="/Imagenes/borrar.jpg"></a></p>
            <%}
        }
    }
            %>
            <table align="center">
                <tr ><td bgcolor="#79A9D9" align="center" valign="middle" width="235px"><span id="blanco">Notas</span><br></td></tr>
            </table>
            <%
    Set ss = u.getNotes();
    Iterator itN = ss.iterator();
    if (ss.isEmpty()) {
            %><p align="center"> <span id="azul">No hay Notas</span></p><%            } else {
                while (itN.hasNext()) {
                    Note n = (Note) itN.next();
            %>
            <p align="center"><a href="" id="colorEvento" name="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>Nota: <% out.print(n.getNote());%></span></a><a href="./Delete?type=note&idNote=<%=n.getIdNote()%>&redir=P1"><img src="/Imagenes/borrar.jpg"></a></p>
            <%
        }
    }
            %>

            <p align="center"> <a href="./FrontController?redir=NT"><img src="./Imagenes/nueva_tarea.gif"/></a>
            <a href="./FrontController?redir=NN"><img src="./Imagenes/nueva_nota.gif"></a></p>

            <table align="center" id="tablaAvisos">
                <tr ><td bgcolor="#79A9D9" align="center" valign="middle" width="235px"><span id="blanco">Avisos</span><br></td></tr>
            </table>
            <table  align="center">
                <%
    List<String> list = new Planner().createNotices(u);
    if (!list.isEmpty()) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String notices = (String) it.next();
            if (notices.contains("móvil")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?error=false&redir=FM" id="aviso"><%=notices%></a></td></tr><%
                            } else if (notices.contains("electricidad")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?redir=NFE" id="aviso"><%=notices%></a></td></tr><%
                            } else if (notices.contains("agua")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?redir=NFA" id="aviso"><%=notices%></a></td></tr><%
                            } else if (notices.contains("seguro")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?redir=NFS" id="aviso"><%=notices%></a></td></tr><%
                            } else if (notices.contains("hipoteca")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?redir=NFH" id="aviso"><%=notices%></a></td></tr><%
                            } else if (notices.contains("sueldo")) {
                %><tr><td width="280px" align="center"><a href="./FrontController?redir=NS" id="aviso"><%=notices%></a></td></tr><%
                        }
                    }
                } else {
                %><tr id="avisos"><td align="center"><span id="rojo">No hay Avisos</span></td></tr><%    }
                %>
            </table>
        </div>

        <br>

        <div id="accesos">
            <table align="center">
                <tr ><td bgcolor="#79A9D9" align="center" valign="middle" width="290px"><span id="blanco">Accesos frecuentes</span><br></td></tr>
            </table>

            <p align="center"><a href="./FrontController?redir=NC" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir cuenta</span></a></p>
            <p align="center">  <a href="./FrontController?redir=NCT" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir teléfono</span></a></p>
            <p align="center"> <a href="./FrontController?error=false&redir=FM" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir factura de Telefonía</span></a> </p>
            <p align="center"> <a href="./FrontController?redir=P" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>Añadir Contratos laborales</span></a> </p>
        </div>




    </div>

</div>

<div id="abajo" align="center"><p>&copy;   Domeco &mdash; Proyecto Informático A&ntilde;o 2009 </p></div>
</td>
</tr>

</table>


<%} else {%>
<jsp:forward page="./index.jsp"></jsp:forward>
<%}%>



</body>
</html>
